﻿using BW.IBLL;
using BW.Model;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.StaticFiles;
using NPOI.XSSF.UserModel;

namespace BW.API.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class kangfuController : ControllerBase
    {
        public kangfuController(IkabgfuBLL bll,ILogger<kangfuController> logger)
        {
            Bll = bll;
            Logger = logger;
        }

        public IkabgfuBLL Bll { get; }
        public ILogger<kangfuController> Logger { get; }

        /// <summary>
        /// 批量修改状态
        /// </summary>
        /// <param name="idstring">参数</param>
        /// <returns>返回受影响行数</returns>
        [HttpGet]
        public async Task<int> plxiugaizhuangtai(string idstring)
        {
            try
            {
                Logger.LogInformation("批量修改状态成功");
                return await Bll.plxiugaizhuangtai(idstring);
            }
            catch (Exception)
            {
                Logger.LogError("批量修改状态出错了");
                throw;
            }
        }
        /// <summary>
        /// 逻辑删除
        /// </summary>
        /// <param name="id">参数</param>
        /// <returns>返回受影响行数</returns>
        [HttpGet]
        public Task<int> shanchu(int id)
        {
            try
            {
                Logger.LogInformation("逻辑删除成功");
                return Bll.shanchu(id);
            }
            catch (Exception)
            {
                Logger.LogError("逻辑删除失败");
                throw;
            }
        }
        /// <summary>
        /// 康复添加
        /// </summary>
        /// <param name="m">参数</param>
        /// <returns>返回受影响行数</returns>
        [HttpPost]
        public Task<int> tinajia(kangfuModel m)
        {
            try
            {
                Logger.LogInformation("康复添加成功");
                return Bll.tinajia(m);
            }
            catch (Exception)
            {
                Logger.LogError("康复添加失败");
                throw;
            }
        }
        /// <summary>
        /// 康复显示
        /// </summary>
        /// <param name="name">名称</param>
        /// <param name="pageindex">当前页</param>
        /// <param name="pagesize">页容量</param>
        /// <returns>返回受影响行数</returns>
        [HttpGet]
        public dynamic xianshi(string? name, int pageindex, int pagesize)
        {
            try
            {
                Logger.LogInformation("康复显示成功");
                return Bll.xianshi(name, pageindex, pagesize);
            }
            catch (Exception)
            {
                Logger.LogError("康复显示失败");
                throw;
            }
        }
        /// <summary>
        /// 导出
        /// </summary>
        /// <returns>返回受影响行数</returns>
        [HttpGet]
        public IActionResult daochu()
        {
            try
            {
                Logger.LogInformation("康复导出成功");
                var rph = Bll.daochu();
                XSSFWorkbook wb = new XSSFWorkbook();
                var dingyi = wb.CreateSheet("康复表");
                var rwo0 = dingyi.CreateRow(0);
                rwo0.CreateCell(0).SetCellValue("项目编号");
                rwo0.CreateCell(1).SetCellValue("项目名称");
                rwo0.CreateCell(2).SetCellValue("项目状态");
                rwo0.CreateCell(3).SetCellValue("项目人数");
                rwo0.CreateCell(4).SetCellValue("项目时长");

                int index = 1;
                foreach (var item in rph)
                {
                    var row = dingyi.CreateRow(index);
                    row.CreateCell(0).SetCellValue(item.bianhoa);
                    row.CreateCell(1).SetCellValue(item.name);
                    row.CreateCell(2).SetCellValue(item.zhuangtai);
                    row.CreateCell(3).SetCellValue(item.renshu);
                    row.CreateCell(4).SetCellValue(item.shichang);
                    index++;
                }
                byte[] bs = null;
                using (MemoryStream ms = new MemoryStream())
                {
                    wb.Write(ms);
                    bs = ms.ToArray();
                }
                var mime = new FileExtensionContentTypeProvider().Mappings[".xlsx"];
                return File(bs, mime, "康复文件.xlsx");
            }
            catch (Exception)
            {
                Logger.LogError("康复导出失败");
                throw;
            }
        }
    }
}
